
  import * as icons from '@ant-design/icons'
  import React from 'react'
  import getViews from './getViews'
  import lazyComponent from './lazyComponent'

  /**
     * 使用递归思想，完成将权限数据转成Antd的Menu导航数据结构
     * @param {*} sourceAry 
     * @returns 
  */
  export const tranformMenu=(sourceAry)=>{
    return sourceAry.map(item=>{
        const menuItem={
            key:item.path,
            label:item.name,
        }
        const iconKey=item.icon&&item.icon.replace(/-o$/,"")
        .replace(/^[a-z]/,letter=>letter.toUpperCase())
        .replace(/-[a-z]/,letter=>letter[1].toUpperCase())+"Outlined"
        const vmIcon=icons[iconKey]
        if(vmIcon){
            menuItem.icon=React.createElement(vmIcon)
        }
        if(item.children){
            menuItem.children=tranformMenu(item.children)
        }
        return menuItem
    })
}
/**
 *  element:<组件名></组件名>
 *  const 组件名=React.lazy(()=>import('相对路径'))
*/
export const tranformRoute=(sourceAry,distAry)=>{
    if(sourceAry&&sourceAry.length>0){
        sourceAry.forEach(item=>{
            if(!item.children){
                const p=getViews(`../views/${item.component}.jsx`)
                const ComponentName=React.lazy(p)
                let routeItem={
                     path:item.path,
                     element:lazyComponent(<ComponentName></ComponentName>)
                }
                distAry.push(routeItem)
            }else{
                tranformRoute(item.children,distAry)
            }
        })
    }
}

